<template>
	<view class="default-background">
		<view class="index-page">
			<view class="index-bar">
				<input class="bar-input" v-model="searchText" @change="searchChange" placeholder="搜点什么..." maxlength="20"/>
				<view class="bar-btn">
					 <!-- 有文字时显示输入，否则为新增按钮 -->
					<uni-icons v-if="searchText==''" type="plusempty" class="bar-btn" color="#FFFFFF" size="26" @click="publishHouse"></uni-icons>
					<view v-else class="search-btn" @click="search()">搜索</view>
				</view>
			</view>
			<view class='swiper-bar' v-if="swiperList.length > 0">
				<swiper
					indicator-dots
					circular
					autoplay
					interval=3000 class="swiper">
					<swiper-item v-for="(item,index) in swiperList" :key="index">
						<image :src="item.img" class="img" mode="aspectFill"></image>
					</swiper-item>
				</swiper>
			</view>
			<scroll-view>
			<view class="house-item" v-for="house in this.houseList" @click="getDetail(house.id)">
					<view class="img-group">
						<image :src="house.firstImg" class="first-img" mode="aspectFill"></image>
					</view>
					<view class="text-group">
						<view class="title limit-one-line">{{house.title}}</view>
						<view class="price" v-if="house.type==0">{{moneyFormat(house.price)}}元/每月</view>
						<view class="price" v-if="house.type==1">{{moneyFormat(house.price)}}元</view>
						<view class="introduce limit-one-line">{{house.introduce}}</view>
						<!-- <uni-dateformat :date="house.createTime" format="yyyy-MM-dd hh:mm:ss" class="create-time"></uni-dateformat> -->
					</view>
				</view>
				<view class="page-bottom"></view>
			</scroll-view>
		</view>
	</view>
</template>

<script>
	import config  from "@/config/app.js"
	export default {
		data() {
			return {
				searchText: "",
				page: 1,
				limit: 6,
				houseList: [],
				swiperList: []
			}
		},
		methods: {
			publishHouse() {
				uni.navigateTo({
					url: "./publishHouse"
				})
			},
			async getHouseList() {
				this.$request.request({
					url: "/house/list",
					method: "GET",
					data: {
						page: this.page,
						limit: this.limit,
						searchText: this.searchText
					}
				}).then(res=>{
					if (res.code == 200) {
						for (let i=0; i<res.data.length; i++) {
							res.data[i].firstImg = config.IMG_URL + JSON.parse(res.data[i].pic)[0]
						}
						if (res.data.length <= 0) {
							uni.showToast({
								title: "没有更多数据了~",
								icon: "none"
							})
							return;
						} else {
							this.houseList = this.houseList.concat(res.data)
						}
					}
				})
			},
			async getSwiperList() {
				this.$request.request({
					url: "/imgModel/indexSwiper",
					method: "GET",
					data: {}
				}).then(res=>{
					if (res.code == 200) {
						for (let i=0; i<res.data.imgList.length; i++) {
							res.data.imgList[i].img = config.IMG_URL + res.data.imgList[i].img
						}
						this.swiperList = res.data.imgList;
					}
				})
			},
			search() {			
				uni.navigateTo({
					url: './searchList?searchText=' + this.searchText,
				});
				this.searchText = '';
			},
			getDetail(id) {
				uni.navigateTo({
					url: "./detail?id=" + id
				})
			},
			moneyFormat(money) {
				if (money > 10000) {
					return parseFloat(money/10000).toFixed(2) + "万";
				} else {
					return money;
				}
			}
		},
		onLoad() {
			this.page = 1;
			this.houseList = [];
			this.getHouseList();
			this.getSwiperList();
		},
		onPullDownRefresh() {
			this.page = 1;
			this.houseList = [];
			this.searchText = "";
			this.getHouseList();
			uni.stopPullDownRefresh();
		},
		async onReachBottom() {
			this.page += 1;
			this.getHouseList();
		},
	}
</script>

<style lang="scss" scoped>
	
	.index-page {
		
		.index-bar {
			background-color: #11A8CD;
			display: flex;
			padding: 20rpx;
			
			.bar-input {
				flex: 1;
				background-color: #FFFFFF;
				border-radius: 50rpx;
				height: 60rpx;
				margin: 0 10rpx;
				padding: 0 30rpx;
			}
			
			.bar-btn {
				height: 60rpx;
				display: flex; 
				flex-direction: row; 
				align-items: center;
				justify-content: center;
				
				.add-btn {
						width: 60rpx;
				}
				
				.search-btn {
					width: 100rpx;
					text-align: center;
					line-height: 60rpx;
					height: 60rpx;
					color: #FFFFFF;
					border-radius: 60rpx;
					
				}
			}
		}
		
		.swiper-bar {
			width: 100%;
			height: 400rpx;
			
			.swiper {
				height: 100%;
				
				.img {
					width: 100%;
				}
			}
			
		}
		
		.house-item {
			background-color: #FFFFFF;
			padding: 20rpx;
			display: flex;
			border-bottom: 3rpx solid #F2F1F6;
			
			.img-group {
				
				.first-img {
					width: 300rpx;
					height: 180rpx;
					border-radius: 20rpx;
				}
			}
			
			.text-group {
				padding: 0 20rpx;
				flex: 1;
				width: 360rpx;
				
				.title {
					font-size: 20px;
				}
				
				.price {
					margin-top: 30rpx;
					color: #F6416C;
				}
				
				.introduce {
					height: 40rpx;
					font-size: 16px;
					color: #999999;
				}
				
				.create-time {
					font-size: 16px;
					color: #999999;
				}
				
			}
		}
	}
	
</style>
